<template>
  <div class="ms-list">

    <el-row class="operation">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <icon style="" name="home"></icon>
        </el-breadcrumb-item>
        <el-breadcrumb-item>财务管理</el-breadcrumb-item>
        <el-breadcrumb-item>消费分析</el-breadcrumb-item>
        <el-breadcrumb-item>项目日总消费</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button-group>
        <router-link :to="{path: '/customer/item-daily-consumption-line',
                           query: {data: this.table.list, search: this.search}}" target="_blank">
          显示图表
        </router-link>
      </el-button-group>
    </el-row>

    <ms-search :data="search.list" @click="updateConditions"></ms-search>

    <el-table
      class="element-table"
      size="small" stripe border
      :data="recombineList"
      :show-summary="true"
      :highlight-current-row="true"
      :fit="true">
      <el-table-column prop="itemTitle" label="项目" min-width="100" fixed="left"></el-table-column>
      <!--<el-table-column prop="name" :label='search.list[0].value +"年 项目消费月报"' class-name="el-table-column-left">-->
      <el-table-column
        v-for="n in 31"
        :key="n"
        :prop="n.toString()"
        :label='n+"日"'
        min-width="80" align="right" header-align="center">
      </el-table-column>
      <!--</el-table-column>-->
      <el-table-column prop="total" label="合计" min-width="80" fixed="right" align="right" header-align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.total !== '' && scope.row.total !== undefined">{{ Number(scope.row.total).toFixed(2)
            }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="table.pagination.page"
      :total="table.pagination.totalRow"
      :page-size="table.pagination.pageSize"
      :page-sizes="table.pagination.pageSizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import MsSearch from '@/components/common/Search.vue'
  import MsConfirmButton from '@/components/common/ConfirmButton.vue'
  import Api from '@/api/customer/daily-consumption'
  import ResponseParse from '@/utils/response-parse'
  import { SEARCH_CONDITION } from '@/models/customer/daily-consumption'
  import moment from 'moment'

  const year = new Date().getFullYear()
  const lastMonth = moment().subtract(1, 'months').format('MM')

  export default {
    components: {
      MsSearch,
      MsConfirmButton
    },
    data () {
      return {
        search: {
          list: [],
          condition: {}
        },
        table: {
          loading: true,
          list: [],
          pagination: {}
        },
        collections: []
      }
    },
    created () {
      this.search.list = SEARCH_CONDITION
      this.search.condition.year = year
      this.search.condition.month = lastMonth
      this.table.list = []
      this.requestList()
    },
    computed: {
      recombineList () {
        let list = []
        let items = this.table.list.list
        if (items !== undefined) {
          for (let i = 0; i < items.length; i++) {
            let arrayData = {}
            arrayData['itemTitle'] = items[i].title
            let dataType = 0
            if (this.search.condition === undefined) {
              dataType = 1
            } else {
              dataType = this.search.condition.dataType
            }
            arrayData.total = 0
            for (let j = 1; j < 32; j++) {
              let key = this.search.condition.year * 10000 + this.search.condition.month * 100 + j
              if (this.table.list.map.dailyDataMap[key] !== undefined) {
                if (this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id] !== undefined) {
                  if (dataType === 1) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['costCoin']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 2) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['costMoney']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 3) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['serviceCoin']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 4) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['cpCostCoin']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 5) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['cpCostMoney']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 6) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['jpCostCoin']
                    arrayData.total += arrayData[j.toString()]
                  } else if (dataType === 7) {
                    arrayData[j.toString()] = this.table.list.map.dailyDataMap[key][items[i].title + '_' + items[i].id]['jpCostMoney']
                    arrayData.total += arrayData[j.toString()]
                  }
                } else {
                  arrayData[j.toString()] = 0
                }
              } else {
                arrayData[j.toString()] = 0
              }
            }
            list.push(arrayData)
          }
        }
        return list
      }
    },
    methods: {
      requestList () {
        this.table.loading = true

        const search = Object.assign({}, this.table.pagination, this.search.condition)
        Api.list({search: search}, response => {
          let status = response.status || 0
          let body = response.data[0] || []
          if (status === 200 || status === 204) {
            this.table.list = body
            this.table.pagination = ResponseParse.pagination(response.headers)
//            if (this.table.pagination.totalPage === this.table.pagination.page) {
//              this.tbHeight = ''
//            } else {
//              this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//            }
          } else {
//            this.tbHeight = ''
            this.table.list = []
            this.$message.error(body.error)
          }
          this.table.loading = false
        })
      },
      handleSizeChange (val) {
        this.table.pagination.pageSize = val
        this.requestList()
      },
      handleCurrentChange (val) {
        this.table.pagination.page = val
        this.requestList()
      },
      updateConditions (conditions) {
        this.table.pagination.page = 1
        this.search.condition = conditions
        this.requestList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .element-table td {
    height: 37px;
  }

  /*覆盖样式*/
  /*.el-table-column-left {*/
    /*text-align: left !important;*/
  /*}*/
</style>
